<?php
/**
 * Created by PhpStorm.
 * User: Guangpeng Chen
 * Date: 10-19/0019
 * Time: 15:33
 */
//phpinfo();

define('ROOT_PATH','/var/www/html/new/d/');
define('FX_PATH', ROOT_PATH.'class/');

define('ORIGIN_12301', 1);
define('ORIGIN_16U', 2);


define('CUR_ORIGIN', ORIGIN_12301);//当前平台1票付通，2先行

define('REQUEST_URL', 'http://www.16u.cc/');//请求地址
//define('REQUEST_URL', 'http://www.12301.cc/');//请求地址

//$_ac = '16ucom';
//$_pw = 'c33367701511b4f6020ec61ded352059';

//include ROOT_PATH . 'conf/le.je';
//include ROOT_PATH . 'conf/trans.le.je';//先行数据库连接
//include '/var/www/html/open/conf/wsdl.le.je';
//$le = new \go_sql();
//$le->connect();
//$sql = "UPDATE uu_product_price SET rid=17884 WHERE id=8651 LIMIT 1";
//$le->query($sql);
//exit;

include ROOT_PATH . 'common/func.inc.php';
include FX_PATH . 'Product.class.php';
include FX_PATH . 'ProductSync.class.php';
include FX_PATH . 'SoapInit.class.php';
include FX_PATH . 'MemberAccount.class.php';
include FX_PATH . 'Sync.class.php';

$dbArr = Sync::GetMulDB();

//$sql = "SELECT id,title,sync_id,sync_status,pft_rflex_id from uu_land WHERE pft_rflex_id>0";
//$dbArr['le']->query($sql);
//$lands = array();
//while($row=$dbArr['le']->fetch_assoc()) {
//    $lands[] = $row;
//}
//print_r($lands);

$sql = <<<SQL
SELECT
t.id,
t.landid,
t.title,
t.apply_did,
t.sync_id,
t.sync_status,
t.pft_rflex_id,
l.title as ltitle,
l.sync_id as l_sync_id,
l.sync_status as l_sync_status,
l.pft_rflex_id as l_pft_rflex_id
from uu_jq_ticket t left join uu_land l on t.landid=l.id
where t.pft_rflex_id>0
AND t.apply_did=94 AND t.apply_did <>1433

SQL;

$sql = <<<SQL
SELECT
t.id,
t.landid,
t.title,
t.apply_did,
t.sync_id,
t.sync_status,
t.pft_rflex_id,
l.title as ltitle,
l.sync_id as l_sync_id,
l.sync_status as l_sync_status,
l.pft_rflex_id as l_pft_rflex_id
from uu_jq_ticket t left join uu_land l on t.landid=l.id
where l.id=5567

SQL;

$dbArr['le']->query($sql);
$tickets = array();
while($row=$dbArr['le']->fetch_assoc()) {
//    if (strpos($row['title'], '测试')!==false || strpos($row['ltitle'],'测试')!==false) continue;
    $tickets[] = $row;
}
print_r($tickets);
$sql_ticket = '';
$sql_remote_ticket = '';
$sql_land = $sql_remote_ticket = array();
foreach ($tickets as $key=>$ticket) {
//    $tid = $ticket['pft_rflex_id'];
    if ($key<=80) {
        //pid
//        $sql_remote = "SELECT id FROM uu_jq_ticket WHERE pid={$ticket['pft_rflex_id']} LIMIT 1";
//        $dbArr['trans_le']->query($sql_remote);
//        $dbArr['trans_le']->fetch_assoc();
//        $tid = $dbArr['trans_le']->f('id');

    } else {
        //tid
        $tid = $ticket['pft_rflex_id'];
    }
    if (!$tid) continue;



    $sql_ticket        = "UPDATE uu_jq_ticket SET sync_id=$tid,sync_status=2,pft_rflex_id=0 WHERE id={$ticket['id']} LIMIT 1;\n";
    $dbArr['le']->query($sql_ticket);
    $sql_remote_ticket = "UPDATE uu_jq_ticket SET sync_id={$ticket['id']},sync_status=1,pft_rflex_id=0 WHERE id={$tid} LIMIT 1;\n";
    $dbArr['trans_le']->query($sql_remote_ticket);
    if (!$ticket['l_pft_rflex_id']) continue;
    $sql_land[$ticket['landid']]  = "UPDATE uu_land SET sync_id={$ticket['l_pft_rflex_id']},sync_status=2,pft_rflex_id=0 WHERE id={$ticket['landid']} LIMIT 1;\n";
    $sql_land_remote[$ticket['l_pft_rflex_id']]  = "UPDATE uu_land SET sync_id={$ticket['landid']},sync_status=1,pft_rflex_id=0 WHERE id={$ticket['l_pft_rflex_id']}  LIMIT 1;\n";
}
foreach ($sql_land as $sql) {
    $dbArr['le']->query($sql);
}

foreach ($sql_land_remote as $sql) {
    $dbArr['trans_le']->query($sql);
}
echo 'UPDATE SUCCESS!' . PHP_EOL;
echo $sql_ticket;
echo str_repeat('-', 100) . PHP_EOL;
echo $sql_remote_ticket;
echo str_repeat('-', 100) . PHP_EOL;
echo implode('',$sql_land);
echo str_repeat('-', 100) . PHP_EOL;
echo implode('',$sql_land_remote);
//print_r($tickets);
exit;
